package thread;

public class SyncDemo3 {
    public static void main(String[] args) {
        Foo f1 = new Foo();
        Foo f2 = new Foo();
        /*new Thread(Foo::buy).start();
        new Thread(Foo::buy).start();*/
        new Thread(()->f1.buy()).start();
        new Thread(()->f2.buy()).start();

    }
}
class Foo{
    //public static synchronized void buy(){
    public static void buy(){
        synchronized(Foo.class){
            try {
                Thread t = Thread.currentThread();
                System.out.println(t.getName()+"正在执行buy方法");
                Thread.sleep(5000);
                System.out.println(t.getName()+"运行完毕");
            } catch (InterruptedException e) {

            }
        }

    }

}
